package com.ryan.ohos.extension.util;


import com.ryan.ohos.extension.TextUtils;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.VectorElement;
import ohos.agp.render.Arc;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Display;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.*;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.util.Optional;


/**
 * ResourceTable 资源工具类
 * 通过资源id获取对应资源类型值
 */
public class ResUtil {
    private static final String TAG = "ResUtil";

    private ResUtil() {
    }

	/**
     * 根据资源id获取 String 类型值
     *
     * @param context the context
     * @param id  ResourceTable.String_BA_CHECK_UNAVAILABLE
     * @return string of the given id
     */
    public static String getString(Context context, int id) {
        String result = "";
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getString();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /**
     * 根据资源id获取color int值
     *
     * @param context the context
     * @param id the ResourceTable.Id_
     * @return the int color
     */
    public static int getColor(Context context, int id) {
        int result = 0;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getColor();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /**
     * 通过id获取Color对象
     *
     * @param context the context
     * @param id the ResourceTable.Id_
     * @return the color
     */
    public static Color getColorObj(Context context, int id) {
        Color result = new Color(getColor(context, id));
        return result;
    }

    /**
     * 根据资源id获取 float 类型值
     *
     * @param context the context
     * @param id ResourceTable.Id_
     * @return get the float dimen value
     */
    public static float getFloat(Context context, int id) {
        float result = 0;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getFloat();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /**
     * 根据资源id获取 boolean 类型值
     *
     * @param context the context
     * @param id ResourceTable.Id_
     * @return get the boolean dimen value
     */
    public static boolean getBoolean(Context context, int id) {
        boolean result = false;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getBoolean();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    
    /**
     * 根据资源id获取 string array 类型值
     *
     * @param context the context
     * @param id the string array ResourceTable.Id_
     * @return the string array
     */
    public static String[] getStringArray(Context context, int id) {
        String[] result = null;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getStringArray();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /**
     * 根据资源id获取 int 类型值
     *
     * @param context the context
     * @param id the int array
     * @return the int array
     */
    public static int getInt(Context context, int id) {
        int result = 0;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getInteger();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /**
     * 根据资源id获取 int array 类型值
     *
     * @param context the context
     * @param id the int array
     * @return the int array
     */
    public static int[] getIntArray(Context context, int id) {
        int[] result = null;
        if (context == null) {
            return result;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return result;
        }
        try {
            result = manager.getElement(id).getIntArray();
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return result;
    }

    /**
     * 通过资源ID获取资源路径
     *
     * @param context the context
     * @param id 示例ResourceTable.Id_text_helloworld1
     * @return 返回资源所在路径
     */
    public static String getPathById(Context context, int id) {
        String path = "";
        if (context == null) {
            return path;
        }
        ResourceManager manager = context.getResourceManager();
        if (manager == null) {
            return path;
        }
        try {
            path = manager.getMediaPath(id);
        } catch (IOException e) {
        } catch (NotExistException e) {
        } catch (WrongTypeException e) {
        }
        return path;
    }

    /**
     * 根据资源id获取 VectorElement
     *
     * @param context the context
     * @param id the drawable ResourceTable.Id_
     * @return the vector drawable
     */
    public static VectorElement getVectorDrawable(Context context, int id) {
        if (context == null) {
            return null;
        }
        return new VectorElement(context, id);
    }

    /**
     * 根据资源id获取 PixelMap
     *
     * @param context the context
     * @param id the id
     * @return the pixel map
     */
    public static Optional<PixelMap> getPixelMap(Context context, int id) {
        String path = getPathById(context, id);
        if (TextUtils.isEmpty(path)) {
            return Optional.empty();
        }
        RawFileEntry assetManager = context.getResourceManager().getRawFileEntry(path);
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();
        options.formatHint = "image/png";
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
        try {
            Resource asset = assetManager.openRawFile();
            ImageSource source = ImageSource.create(asset, options);
            return Optional.ofNullable(source.createPixelmap(decodingOptions));
        } catch (IOException e) {
        }
        return Optional.empty();
    }

    /**
     * 创建 ShapeElement
     * @param context
     * @param shape int
     * @param color int
     * @param radius float
     * @return ShapeElement
     */
    public static final ShapeElement getShapeElement(Context context, int shape, int color, float radius) {
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setShape(shape);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(getColor(context, color)));
        shapeElement.setCornerRadius(radius);
        return shapeElement;
    }

    /**
     * 根据资源id 获取PixelMapElement
     *
     * @param context the context
     * @param resId the res id
     * @return the Pixel Map Element
     */
    public static PixelMapElement getPixelMapDrawable(Context context, int resId) {
        Optional<PixelMap> optional = getPixelMap(context, resId);
        return optional.map(PixelMapElement::new).orElse(null);
    }


    /**
     * get the ShapeElement 弧形渐变
     *
     * @param thickness 边框宽度
     * @param inside 设置填充ShapeElement对象的颜色
     * @param border 设置边框宽度的颜色。
     * @param startAngle 表示弧的起始角，0度是3点钟。
     * @param sweepAngle 表示圆弧的扫掠角度，即顺时针旋转。
     * @return the ShapeElement
     */
    public static ShapeElement getCustomArcGradientDrawable(int thickness, Color inside, Color border, float startAngle,float sweepAngle) {
        ShapeElement drawable = new ShapeElement();
        drawable.setShape(ShapeElement.ARC);
        drawable.setRgbColor(RgbColor.fromArgbInt(inside.getValue())); // keep it transparent for main(inner) part
        drawable.setArc(new Arc(startAngle, sweepAngle, false));//useCenter-指示是否绘制从弧的两个端点到其中心的线。
        drawable.setStroke(thickness, RgbColor.fromArgbInt(border.getValue()));
        return drawable;
    }


    /**
     * 通过资源id获取PixelMap 并根据屏幕密度进行缩放
     *
     * @param context the context
     * @param id the id
     * @return the scaled pixel map
     */
    public static Optional<PixelMap> getPixelMapScaled(Context context, int id) {
        String path = getPathById(context, id);
        if (TextUtils.isEmpty(path)) {
            return Optional.empty();
        }
        RawFileEntry assetManager = context.getResourceManager().getRawFileEntry(path);
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();
        options.formatHint = "image/png";
        ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();

        try {
            Resource asset = assetManager.openRawFile();
            ImageSource source = ImageSource.create(asset, options);
            float width = source.getImageInfo().size.width * getDensityPixel(context);
            float height = source.getImageInfo().size.height * getDensityPixel(context);

            decodingOptions.desiredSize = new Size((int) width, (int) height);
            return Optional.ofNullable(source.createPixelmap(decodingOptions));
        } catch (IOException e) {
        }
        return Optional.empty();
    }

    /**
     * 返回设备密度(以像素表示显示密度)。
     * @param context
     * @return float
     */
    private static float getDensityPixel(Context context) {
        Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(context);
        return display.get().getAttributes().densityPixels;
    }

	
}

